import { ElMessage } from "element-plus";
import "element-plus/es/components/message/style/css";

import { ElMessageBox } from "element-plus";
import "element-plus/es/components/message-box/style/css";

/**
 * 消息提示队列
 * ElMessage 自动关闭前的时间内，重复的错误提示只显示一次
 */
let msgArr: string[] = [];
export const $singleMsg = (
  text: string = "",
  type: "error" | "success" | "warning" | "info" = "success"
): void => {
  if (!msgArr.some(item => item === text)) {
    msgArr.push(text);
    ElMessage({
      message: text,
      customClass: "custom-message",
      type,
      onClose: () => {
        msgArr = msgArr.filter((item: string) => item !== text);
      },
    });
  }
};

/**
 * confirm 确认弹窗
 */
export const $confirm = async ({ tip, title, type, sureText, cancelText }: any) => {
  await ElMessageBox.confirm(tip, title, {
    confirmButtonText: sureText || "确定",
    cancelButtonText: cancelText || "取消",
    type: type || "warning",
  });
};

/**
 * element - alert
 * 对于同样的信息不会触发两次
 */
let alertMsgQueue: string[] = [];
export const $alert = async ({ tip, title, type, callback, sureText, cancelText }: any) => {
  if (!alertMsgQueue.some(item => item === tip)) {
    alertMsgQueue.push(tip);
    await ElMessageBox.alert(tip, title, {
      confirmButtonText: sureText || "确定",
      cancelButtonText: cancelText || "取消",
      type,
      callback: () => {
        alertMsgQueue = alertMsgQueue.filter((item: string) => item !== tip);
        callback && callback();
      },
    });
  } else {
    return Promise.reject();
  }
};
